/*******************************************************************************
 * Copyright (c) 2004 Coffee-Bytes.com and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/cpl.php
 * 
 * Contributors:
 *     Coffee-Bytes.com - initial API and implementation
 *******************************************************************************/
package com.cb.eclipse.folding;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.cb.eclipse.folding.java.JavaSettings;
import com.cb.eclipse.folding.java.preferences.Defaults;
import com.cb.eclipse.folding.theme.Images;

/**
 * The main plugin class for the FoldingPlugin.
 */
public class FoldingPlugin extends AbstractUIPlugin {
    public static final String PLUGIN_ID= "com.cb.eclipse.folding";
    public static final int BAD_LOCATION_EXCEPTION= 7007;
    public static final int NULL_DOCUMENT = 7008;
    public static final int INVALID_INPUT = 7009;
    public static final int COMPUTE_PROJECTIONS_CALL = 7010;
    
	// The shared instance.
	private static FoldingPlugin plugin;
	//Resource bundle.
	private ResourceBundle resourceBundle;
	private boolean resourceBundleBuilt;
	
	
	private Images images;
	
	private JavaSettings javaDomain;

    public FoldingPlugin() {
        super();
        plugin= this;
    }
    
	public static void restoreToDefaults() {
		Defaults.restoreDefaults(getPrefs());
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
        Defaults.applyDefaults(getPreferenceStore());
        javaDomain = new JavaSettings(getPreferenceStore());
        images = new Images();
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
	}
	
	/**
	 * Returns the shared instance.
	 */
	public static FoldingPlugin getDefault() {
		return plugin;
	}
	
	public static Images getImages() {
		return getDefault().images;
	}
	
	public static JavaSettings getJavaDomain() {
		return getDefault().javaDomain;
	}

	public static IPreferenceStore getPrefs() {
		return getDefault().getPreferenceStore();
	}

	/**
	 * Returns the string from the plugin's resource bundle, or 'key' if not
	 * found.
	 */
	public static String getMessage(String key) {
		ResourceBundle bundle = FoldingPlugin.getDefault().getResourceBundle();
		try {
			return (bundle != null) ? bundle.getString(key) : key;
		}
		catch (MissingResourceException e) {
			return key;
		}
	}

	public static boolean getBoolean(String key) {
		boolean result = FoldingPlugin.getPrefs().getBoolean(key);
		return result;
	}
	

	/**
	 * Returns the plugin's resource bundle,
	 */
	public ResourceBundle getResourceBundle() {
		if(!resourceBundleBuilt) {
			try {
				resourceBundle = ResourceBundle.getBundle("com.cb.eclipse.folding.FoldingResources");
			}
			catch (MissingResourceException x) {
				resourceBundle = null;
			}
			
			resourceBundleBuilt=true;
		}
		
		return resourceBundle;
	}
}